package com.ysq.frame;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * 键盘监听
 * @author Administrator
 *
 */
public class KeyCommand extends KeyAdapter{
	MainPanel mainPanel;
	
	public KeyCommand(MainPanel mainPanel){
		this.mainPanel = mainPanel;
	}
	@Override
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if (keyCode == KeyEvent.VK_UP) {
			if(mainPanel.increaseY == 0){
				mainPanel.increaseX = 0;
				mainPanel.increaseY = -1;
			}
		}else if(keyCode == KeyEvent.VK_DOWN) {
			if(mainPanel.increaseY == 0){
				mainPanel.increaseX = 0;
				mainPanel.increaseY = 1;
			}
		}else if(keyCode == KeyEvent.VK_LEFT) {
			if(mainPanel.increaseX == 0){
				mainPanel.increaseX = -1;
				mainPanel.increaseY = 0;
			}
		}else if(keyCode == KeyEvent.VK_RIGHT) {
			if(mainPanel.increaseX == 0){
				mainPanel.increaseX = 1;
				mainPanel.increaseY = 0;
			}
		}else if(e.getKeyCode() == 80) {
			if(mainPanel.isRunning){//正在运行
				mainPanel.timer.stop();
				mainPanel.isRunning = false;
				mainPanel.statusJLabel.setText("按P键继续");
			}else{
				mainPanel.timer.start();
				mainPanel.isRunning = true;
				mainPanel.statusJLabel.setText("按P键暂停");
			}
			mainPanel.repaint();
		}else if(e.getKeyCode() == 107) {//speed +
			int speed = mainPanel.getSpeed();
			if(speed < 14){//最大14级
				mainPanel.setSpeed(speed + 1);
			}
		}else if(e.getKeyCode() == 109) {//speed -
			int speed = mainPanel.getSpeed();
			if(speed >1){
				mainPanel.setSpeed(speed - 1);
			}
		}
	}
}
